﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TinyIM2013.Packets
{
    public class PSend_0825:PSend
    {
        public byte[] TeaKey { get; set; }
        public PHeader PacketHead { get; set; }
        public bool TryParse(byte[] packetData, out PSend packet)
        {
            packet = new PSend_0825();
            byte[] teaKey = GetTEAKey(packetData);
            byte[] encryptedData = GetEnctyptedData(packetData);
            byte[] decryptedData = TEAUtility.Decrypt(encryptedData, 0, encryptedData.Length, teaKey);
            if (decryptedData != null)
            {
                packet.PacketHead = PacketUtility.GetPHead(packetData);
                TeaKey = teaKey;
                return true;
            }
            else
            {
                return false;
            }
        }
        public byte[] GetTEAKey(byte[] packetData)
        {
            byte[] result = new byte[16];
            Array.Copy(packetData, 26, result, 0, 16);
            return result;
        }
        public byte[] GetEnctyptedData(byte[] packetData)
        {
            int enctyptedDataLength = packetData.Length - 43;
            if (enctyptedDataLength >= 16)
            {
                byte[] result = new byte[enctyptedDataLength];
                Array.Copy(packetData, 42, result, 0, enctyptedDataLength);
                return result;
            }
            return new byte[0];
        }
        public byte[] DecryptDataWithTEAKey(byte[] packetData)
        {
            return TEAUtility.Decrypt(GetTEAKey(packetData),0,72,GetEnctyptedData(packetData));
        }



        public byte[] EncryptedData
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public byte[] DecryptedData
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public byte[] GeneratePacket()
        {
            throw new NotImplementedException();
        }

        public void SendPacket()
        {
            throw new NotImplementedException();
        }
    }
   
    
}
